/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

window.onload = function(){
    var test = document.getElementById("test");
    test.onclick = function(e){
        var currentObj = e.target;
        var classname = currentObj.className;
        if (classname == "isShow"){
            var productId = currentObj.getAttribute("proId");
            updateStatus(productId);
        } else if (classname =="isSave"){
            var productId = currentObj.getAttribute("proId");
            var productName = document.getElementById("txtName" + productId);
            var productDescription = document.getElementById("txtdescription" + productId);
            var productPrice = document.getElementById("txtprice" + productId);
            var productQuantity = document.getElementById("txtQuantityInStock" + productId);
            saveProduct(productId,productName, productDescription, productPrice, productQuantity);
        } else if (classname == "isActive"){
            var userName = currentObj.getAttribute("username");
            updateStatusUser(userName);
        }

    }
}

function updateStatus(productId){
    var xmlhttp;
    xmlhttp = new XMLHttpRequest();
    xmlhttp.open ("GET", contextPath + "/AdminController?action=ShowProduct&proid="+productId, true);
    xmlhttp.send();

    xmlhttp.onreadystatechange = function(e){
        var controlContainer = document.getElementById("test");
        var current = e.target;
        if (current.readyState == 4 && current.status == 200){
            var proString = xmlhttp.responseText;
            controlContainer.innerHTML = "";
            controlContainer.innerHTML = proString;
        }
    }
}
function updateStatusUser(username){
    var xmlhttp;
    xmlhttp = new XMLHttpRequest();
    xmlhttp.open ("GET", contextPath + "/AdminController?action=ShowUser&username="+username, true);
    xmlhttp.send();

    xmlhttp.onreadystatechange = function(e){
        var controlContainer = document.getElementById("test");
        var current = e.target;
        if (current.readyState == 4 && current.status == 200){
            var userString = xmlhttp.responseText;
            controlContainer.innerHTML = "";
            controlContainer.innerHTML = userString;
        }
    }
}

function saveProduct(productId, proName, proDes, proPrice, proQuantity){
    var xmlhttp;
    xmlhttp = new XMLHttpRequest();
    xmlhttp.open ("GET", contextPath + "/AdminController?action=SaveProduct&proId="+productId+"&proName="+proName.value+"&proDes="+proDes.value + "&proPrice="+proPrice.value + "&proQuantity=" + proQuantity.value, true);
    xmlhttp.send();

    xmlhttp.onreadystatechange = function(e){
        var controlContainer = document.getElementById("test");
        var current = e.target;
        if (current.readyState == 4 && current.status == 200){
            var proString = xmlhttp.responseText;
            controlContainer.innerHTML = "";
            controlContainer.innerHTML = proString;
        }
    }
}

function validate(){
    
}